package cate.game.play.skill;


import cp.solution.util.collection.MapKit;
import easy.java.dev.note.NoteClass;
import easy.java.dev.note.NoteField;

import java.util.HashMap;
import java.util.Map;


@NoteClass("战斗者CD控制")
public class FighterCD {

    @NoteField("Map<技能ID， 解锁回合>")
    private Map<Integer, Integer> record = new HashMap<>();


    /** 处理技能cd */
    public void handleSkillCD(Skill skr, int roundIndex) {
        int cdValue = skr.cfg.getCD();
        int preRoundIndex = record.getOrDefault(skr.tid, roundIndex);
        record.put(skr.tid, cdValue + preRoundIndex);
    }

    /** 指定回合是否可用 */
    public boolean available(Skill skill, int curRoundIndex) {
        if (curRoundIndex < skill.cfg.row.unlockRound) {
            return false;
        }
        Integer unlockRoundIndex = record.get(skill.tid);
        if (unlockRoundIndex == null) {
            return true;
        }
        return curRoundIndex >= unlockRoundIndex;
    }

    /** 刷新指定技能的cd */
    public void refresh(int tid){
        record.remove(tid);
    }

    /**
     * 减cd
     * @param tid
     * @param round
     */
    public void reduce(int tid, int round) {
        Integer curAvailableRound = record.get(tid);
        if (curAvailableRound == null) {
            return;
        }
        record.put(tid, curAvailableRound - round);
    }

    public FighterCD copy() {
        FighterCD cp = new FighterCD();
        cp.record = MapKit.clone(this.record);
        return cp;
    }

    /**
     * 技能冷却中
     * @param lastRound
     * @param cd
     * @param curRound
     * @return
     */
    public static boolean inCD(int lastRound, int cd, int curRound) {
        if (lastRound <= 0) {
            return false;
        }
        if (cd <= 0) {
            return false;
        }
        return lastRound + cd > curRound;
    }

}
